
package com.jf.cloud.order.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jf.cloud.order.vo.PurchaseOrderInboundExcelVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商品excel导入监听
 *
 * 有个很重要的点 SpuExcelListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
 *
 * @author zz
 * @date 2021/5/14
 */

public class PurchaseOrderInboundExcelListener extends AnalysisEventListener<PurchaseOrderInboundExcelVO> {

    private Map<String, List<String>> errorMap;
    private List<PurchaseOrderInboundExcelVO> list;
    public static String OTHER = "other";
    public static String ERROR_ROWS = "error_rows";
    public static String IMPORT_ROWS = "import_rows";

    public PurchaseOrderInboundExcelListener() {
    }

    public PurchaseOrderInboundExcelListener(Map<String, List<String>> errorMap, List<PurchaseOrderInboundExcelVO> list) {
        errorMap.put(OTHER, new ArrayList());
        errorMap.put(ERROR_ROWS, new ArrayList());
        errorMap.put(IMPORT_ROWS, new ArrayList());
        this.errorMap = errorMap;
        this.list = list;
    }

    /**
     * 这个每一条数据解析都会来调用
     */

    @Override
    public void invoke(PurchaseOrderInboundExcelVO userExcelDTO, AnalysisContext analysisContext) {
//        boolean isSave = Objects.nonNull(seq) && !Objects.equals(seq, userExcelDTO.getSeq()) && list.size() > BATCH_COUNT;
//        if (isSave) {
//            saveData();
//        }
//        seq = userExcelDTO.getSeq();
        if (StrUtil.isBlank(userExcelDTO.getPartyCode()) && Objects.isNull(userExcelDTO.getPurchaseStock())){
            return;
        }
        if (StrUtil.isBlank(userExcelDTO.getSpuName())) {
            userExcelDTO.setSpuName("");
        }
        if (Objects.isNull(userExcelDTO.getActualStock())) {
            userExcelDTO.setActualStock(0);
        }
        list.add(userExcelDTO);
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }

//
//    public List<PurchaseSpuExcelVO> getList() {
//        return this.list;
//    }

}
